package com.kuangstudy.controller.string.creatorid;

import com.kuangstudy.entity.Product;
import com.kuangstudy.service.creatorid.IdCreatorService;
import com.kuangstudy.service.creatorid.SequenceUtils;
import com.kuangstudy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/5/20 18:46
 */
@RestController
public class ProductController {

    @Autowired
    private IdCreatorService redisCreatorIdService;
    @Autowired
    private SequenceUtils sequenceUtils;

    @PostMapping("/product/creator")
    public R creatorIds() {
        // 1:创建产品的分布式全局ID
        Long productid = redisCreatorIdService.incrmentid();
        // 2:创建产品
        Product product = new Product();
        product.setId(productid);
        product.setTitle("title:" + productid);
        System.out.println("保存产品是：" + product.toString());
        // 3: 存储到指定的数据库中 ,方便测试1024修改成10
        int table = (int) (productid % 10);
        System.out.println("保存到的数据库表是：product_" + table);
        return R.ok().data("tablename", "product_" + table);
    }

    @PostMapping("/product/creator2")
    public R creatorIds2() {
        // 1:创建产品的分布式全局ID
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            String sequence = sequenceUtils.getSequence();
            ids.add(sequence);
        }
        return R.ok().data("ids", ids);
    }

}
